/*
 * @描述: 
 * @创建者: kilomChou
 * @Date: 2021-03-16 15:29:17
 * @最后编辑: kilomChou
 * @LastEditTime: 2021-05-06 08:46:35
 */
import { ProtoVo } from "../../data/Proto";

/** 协议派发器 */
export default class ProtoDispatcher {

    private _cbMap: Map<keyof ProtoVo.Def, (data: any) => void> = new Map()

    /** 监听协议 协议只能监听一次，重复监听会覆盖前一次的 */
    on<Proto extends keyof ProtoVo.Def>(proto: Proto, cb: (data: ProtoVo.Def[Proto]) => any) {
        this._cbMap.set(proto, cb)
    }

    /** 接收到协议 */
    onMsg<Proto extends keyof ProtoVo.Def>(proto: Proto, data: ProtoVo.Def[Proto]) {
        let cb = this._cbMap.get(proto);
        cb && cb(data);
    }

}